package com.cin.cma.client.service.impl;


import cn.hutool.http.HttpRequest;
import com.cin.cma.client.service.IFinixxProxyService;
import com.cin.cma.client.service.IHttpService;
import com.cin.cma.common.model.SocketResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.charset.Charset;

@Slf4j
@Service
public class FinixxProxyServiceImpl implements IFinixxProxyService {
    @Autowired
    private IHttpService httpService;

    /**
     * 请求数据
     *
     * @param url  请求路径
     * @param body 请求内容
     * @return 返回内容
     */
    @Override
    public SocketResponse request(String url, String body) {
        return this.httpService.request(url, body, (socketRequest) -> {
            HttpRequest request = HttpRequest.post(socketRequest.getRequestUrl())
                    .contentType("text/json; charset=GB2312")
                    .timeout(30000)
                    .charset(Charset.forName("GB2312"))
                    .body(socketRequest.getRequestBody());
            return request.execute();
        });
    }

    /**
     * 请求数据
     *
     * @param body 请求内容
     * @return 返回内容
     */
    @Override
    public SocketResponse request(String body) {
        return this.request(null, body);
    }
}
